<?xml version="1.0" encoding="UTF-8"?>
<%@page contentType="text/html; charset=UTF-8"
		import="org.hibernate.Criteria,
				org.hibernate.criterion.Restrictions,
				org.apache.commons.lang.StringUtils" %>
<%
	String status = request.getParameter("status");
	
	int realStatus = -1;
	if (!StringUtils.isEmpty(status)) {
		realStatus = Integer.valueOf(status);
	}

	// Liste des commandes
	Session hSession = HibernateUtil.getSessionFactory().getCurrentSession();
	Criteria crit = hSession.createCriteria(Order.class);
	
	switch (realStatus) {
	case Order.STATUS_ORDERED:
		crit.add(Restrictions.isNull("shippingDate"));
		break;
	
	case Order.STATUS_SHIPPED:
		crit.add(Restrictions.isNotNull("shippingDate"));
		break;
	}
	
	Collection<Order> orders = crit.list();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
	<title>Bookstore :: Back office :: Commandes</title>
	<%@include file="include.jsp" %>
</head>

<body>
	<%@include file="commun.jsp" %>
	
	<div class="contenuAvecMenu">
		<h2>Commandes</h2>
		<div class="boite bleue">
			<table>
				<tr>
					<th class="etroite">Identifiant</th>
					<th class="large">Utilisateur</th>
					<th class="moyenne">Statut</th>
					<th class="etroite">Date de commande</th>
					<th class="etroite">Date d'envoi</th>
					<th class="large">Actions</th>
				</tr>
				<% for (Order order : orders) { %>
				<tr>
					<td class="centree">
						<a href="javascript:viewOrder(<%=order.getId()%>);" title="Voir le detail">
							<%=order.getId()%>
						</a>
					</td>
					<td><%=order.getUser()%></td>
					
					<% if (order.calculateStatus() == Order.STATUS_ORDERED) { %>
						<td class="centree"><strong>Reglee</strong></td>
						<td class="centree"><%=FormatUtil.formatDate(order.getOrderDate())%></td>
						<td class="centree">N/A</td>
						<td class="centree">
							<img src="../resources/images/lorry.png" alt="" />
							<a href="SaveShippingDate.do?id=<%=order.getId()%>&amp;status=<%=status%>">
								Marquer comme envoyee
							</a>
						</td>
					<% } else if (order.calculateStatus() == Order.STATUS_SHIPPED) { %>
						<td class="centree">Envoyee</td>
						<td class="centree"><%=FormatUtil.formatDate(order.getOrderDate())%></td>
						<td class="centree"><%=FormatUtil.formatDate(order.getShippingDate())%></td>
						<td class="centree">-</td>
					<% } else { %>
						<td class="centree">N/A</td>
						<td class="centree">N/A</td>
						<td class="centree">N/A</td>
						<td class="centree">-</td>
					<% } %>
				</tr>
				<% } %>
			</table>
		</div>
	</div>
</body>
</html>
